package com.lwf.smartseniorcare.controller;

import com.lwf.smartseniorcare.dto.ActivityRecordDTO;
import com.lwf.smartseniorcare.service.ActivityRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/activity-records")
public class ActivityRecordController {

    @Autowired
    ActivityRecordService activityRecordService;

    // Get all activity records - only accessible to ADMIN
    @PreAuthorize("hasRole('ADMIN')")
    @GetMapping
    public ResponseEntity<List<ActivityRecordDTO>> getAllActivityRecords() {
        List<ActivityRecordDTO> records = activityRecordService.getAllActivityRecords();
        return ResponseEntity.ok(records);
    }

    // Get activity record by ID
    @GetMapping("/{id}")
    public ResponseEntity<ActivityRecordDTO> getActivityRecordById(@PathVariable Long id) {
        ActivityRecordDTO record = activityRecordService.getActivityRecordById(id);
        return ResponseEntity.ok(record);
    }

    // Create activity record - only accessible to ADMIN, DOCTOR, and CARETAKER
    @PreAuthorize("hasRole('ADMIN') or hasRole('DOCTOR') or hasRole('CARETAKER')")
    @PostMapping
    public ResponseEntity<ActivityRecordDTO> createActivityRecord(@RequestBody ActivityRecordDTO activityRecordDTO) {
        ActivityRecordDTO createdRecord = activityRecordService.createActivityRecord(activityRecordDTO);
        return ResponseEntity.ok(createdRecord);
    }

    // Update activity record - only accessible to ADMIN, DOCTOR, and CARETAKER
    @PreAuthorize("hasRole('ADMIN') or hasRole('DOCTOR') or hasRole('CARETAKER')")
    @PutMapping("/{id}")
    public ResponseEntity<ActivityRecordDTO> updateActivityRecord(@PathVariable Long id, @RequestBody ActivityRecordDTO activityRecordDTO) {
        ActivityRecordDTO updatedRecord = activityRecordService.updateActivityRecord(id, activityRecordDTO);
        return ResponseEntity.ok(updatedRecord);
    }

    // Delete activity record - only accessible to ADMIN
    @PreAuthorize("hasRole('ADMIN')")
    @DeleteMapping("/{id}")
    public ResponseEntity<?> deleteActivityRecord(@PathVariable Long id) {
        activityRecordService.deleteActivityRecord(id);
        return ResponseEntity.ok("Activity record deleted successfully");
    }

    // Get activity records by elderly
    @GetMapping("/elderly/{elderlyId}")
    public ResponseEntity<List<ActivityRecordDTO>> getActivityRecordsByElderly(@PathVariable Long elderlyId) {
        List<ActivityRecordDTO> records = activityRecordService.getActivityRecordsByElderly(elderlyId);
        return ResponseEntity.ok(records);
    }

    // Get activity records by elderly and date range
    @GetMapping("/elderly/{elderlyId}/date-range")
    public ResponseEntity<List<ActivityRecordDTO>> getActivityRecordsByElderlyAndDateRange(
            @PathVariable Long elderlyId,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Date startDate,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Date endDate) {
        List<ActivityRecordDTO> records = activityRecordService.getActivityRecordsByElderlyAndDateRange(elderlyId, startDate, endDate);
        return ResponseEntity.ok(records);
    }

    // Get activity records by elderly and type
    @GetMapping("/elderly/{elderlyId}/type/{activityType}")
    public ResponseEntity<List<ActivityRecordDTO>> getActivityRecordsByElderlyAndType(
            @PathVariable Long elderlyId,
            @PathVariable String activityType) {
        List<ActivityRecordDTO> records = activityRecordService.getActivityRecordsByElderlyAndType(elderlyId, activityType);
        return ResponseEntity.ok(records);
    }

    // Get recent activity records by elderly
    @GetMapping("/elderly/{elderlyId}/recent")
    public ResponseEntity<List<ActivityRecordDTO>> getRecentActivityRecordsByElderly(@PathVariable Long elderlyId) {
        List<ActivityRecordDTO> records = activityRecordService.getRecentActivityRecordsByElderly(elderlyId);
        return ResponseEntity.ok(records);
    }
}
